{
 "metadata": {
  "signature": "sha256:28a101e7e053e42bc56b1d3f4055fd867facdd4747f0b2515d38124aef3870a1"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: plotting values with masked arrays\n",
      "======================================================================\n",
      "\n",
      "From time to time one might end up with \"meaningless\" data in an array.\n",
      "Be it because a detector didn't work properly or for an other reason. Or\n",
      "one has to deal with data in completely different ranges. In both cases\n",
      "plotting all values will screw up the plot. This brief example script\n",
      "addresses this problem and show one possible solution using masked\n",
      "arrays. See 'masked\\_demo.py' in the matplotlib examples for a\n",
      "reference, too."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "\n",
      "y_values = [0,0,100,97,98,0,99,101,0,102,99,105,101]\n",
      "x_values = [0,1,2,3,4,5,6,7,8,9,10,11,12]\n",
      "\n",
      "#give a threshold\n",
      "threshold = 1\n",
      "\n",
      "#prepare for masking arrays - 'conventional' arrays won't do it\n",
      "y_values = np.ma.array(y_values)\n",
      "#mask values below a certain threshold\n",
      "y_values_masked = np.ma.masked_where(y_values < threshold , y_values)\n",
      "\n",
      "#plot all data\n",
      "plt.subplots_adjust(hspace=0.5)\n",
      "plt.subplot(311)\n",
      "plt.plot(x_values, y_values,'ko')\n",
      "plt.title('All values')\n",
      "plt.subplot(312)\n",
      "plt.plot(x_values, y_values_masked,'ko')\n",
      "plt.title('Plot without masked values')\n",
      "ax = plt.subplot(313)\n",
      "ax.plot(x_values, y_values_masked,'ko')\n",
      "#for otherwise the range of x_values gets truncated:\n",
      "ax.set_xlim(x_values[0], x_values[-1])\n",
      "plt.title('Plot without masked values -\\nwith full range x-axis')\n",
      "\n",
      "savefig('masked_test.png')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The resulting figure might illustrate the problem - note the different scales in all three subplots:\n",
      "\n",
      "![](files/attachments/Matplotlib_Plotting_values_with_masked_arrays/masked_test.png)"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}